import 'package:flutter/material.dart';
import 'package:charts_flutter/flutter.dart' as charts;
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:material_collection_app/utils/utils.dart';

class RingChart2 extends StatefulWidget {
  final List data;
  final int total;
  RingChart2({Key key, this.data, this.total}) : super(key: key);

  @override
  State<StatefulWidget> createState() => MyState();
}

class TimeSeriesSales {
  final DateTime time;
  final int sales;

  TimeSeriesSales(this.time, this.sales);
}

class MyState extends State<RingChart2> {

  // widget.data[0]

  initChart() {
    if(widget.data.length > 0) {
      List colorList = ['#20B0F8', '#1BC85E', '#7877FF', '#FEA24B'];

      List<LinearSales> serialdata = [];

      for(var i=0; i<widget.data.length; i++) {
        var item = widget.data[i];
        serialdata.add(
          new LinearSales(item['department'], (i+1), item['sum'], Utils.hexToColor(colorList[i])),
        );
      }

      List<charts.Series<LinearSales, int>> seriesList =  [
        new charts.Series<LinearSales, int>(
          id: 'Sales',
          domainFn: (LinearSales sales, _) => sales.year,
          measureFn: (LinearSales sales, _) => sales.sales,
          data: serialdata,
          colorFn: (LinearSales sales, _) =>   charts.ColorUtil.fromDartColor(sales.color),
          outsideLabelStyleAccessorFn: (LinearSales sales, _) =>
          charts.TextStyleSpec(
              fontSize: 12,
              color: charts.ColorUtil.fromDartColor(sales.color),
          ),
          insideLabelStyleAccessorFn: (LinearSales sales, _) =>
          charts.TextStyleSpec(
              color: charts.ColorUtil.fromDartColor(Colors.orange),
          ),
          labelAccessorFn: (LinearSales row, _) => '${row.title} \n${row.sales}',
        )
      ];

      var chart = charts.PieChart(
        seriesList,
        animate: true,
        defaultRenderer: new charts.ArcRendererConfig(
          arcWidth: 30,
          arcRendererDecorators: [new charts.ArcLabelDecorator()]
        )
      );

      return chart;
    }else {
      return Container();
    }
  }

  @override
  Widget build(BuildContext context) {
    ScreenUtil.init(context, width: 750, height: 1334, allowFontScaling: false);

    return  Container(
      child: Stack(
        children: <Widget>[
          initChart(),
          Container(
            child: Align(
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: <Widget>[
                  Container(
                    margin: EdgeInsets.only(top: 20.w),
                    child: Text(widget.total.toString(), style: TextStyle(
                      color: Utils.hexToColor('#20AFF7'),
                      fontSize: 50.w,
                      fontWeight: FontWeight.bold
                    )),
                  ),
                  Text('处置总数', style: TextStyle(
                    color: Utils.hexToColor('#CBCBCB'),
                    fontSize: 12,
                  ))
                ],
              ),
            ),
          )
        ],
      )
    );
  }
}

class LinearSales {
  final String title;
  final int year;
  final int sales;
  final Color color;

  LinearSales(this.title, this.year, this.sales, this.color);
}